<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return['diy']=$this->plugin_setting('diy');
if($_RQ['op']=='main'){
	exi($return);
}
$return['ispay']=array('0'=>'未支付','1'=>'已支付');
/*******op*******/
set_html('title','配送订单');
//paisong_type,指定配送员的类型（0所有，1平台，2商户自己的员工）
if($_RQ['op']=='list'){
	$citypeisong=plugin('jy_weishop')->plugin_setting('citypeisong');
	if($citypeisong['set_peisonguser']!=3){
		exi('当前系统不支持抢单,只能系统指派/店员指派！','error','jy_peisong/index/');
	}
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'paisong_type'=>0,'plugin_name <>'=>'jy_mfuwu','sendtype'=>array(1,4),'send_status'=>array(0,12));
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$order_list=pdo_getslice('jy_weishop_order',$params,array($pindex,$psize),$total,array(),'','createtime desc');
	if($order_list){
		foreach($order_list as &$item){
			$user=pdo_get('jy_weishop_member',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
			if($item['merchid']){
				$store=pdo_get('jy_merch_user',array('id'=>$item['merchid'],'uniacid'=>$_SESSION['uniacid']));
				$store['title']=$store['merchname'];
			}else{
				$store=pdo_get('jy_weishop_store',array('id'=>$item['storeid'],'uniacid'=>$_SESSION['uniacid']));	
			}
			$item['store']=$store;
			$item['goods']=pdo_getall('jy_weishop_order_goods',array('orderid'=>$item['id']));
			$item['user_name']=$user['name'];
			$item['user_mobile']=$user['mobile'];
			$item['createtime']=date('Y-m-d H:i:s', $item['createtime']);
			$store_times=pdo_getcolumn('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['storeid']),'onlinetime');
			$store_times=json_decode($store_times,true);
			$item['serve_time']=$store_times[$item['timeid']];
			$item['paytype']=$return['ispay'][$item['is_pay']];
			//用户地址
			$item['address']=json_decode($item['address'],true);
			if($item['address'] && ($item['address']['name'] || $item['address']['realname'])){
				$address=$item['address'];
			}else{
				$address=pdo_get('core_address',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['addressid']));
			}
			if(!is_array($address['pcd'])){
				$address['pcd']=json_decode($address['pcd'],true);
			}
			$address['name']=$address['name']?:$address['realname'];
			$item['addresstext']=model('core/member')->get_address_str($address);
			$item['order_out']=$address;
			if($item['goods']){
				foreach($item['goods'] as &$g){
					$goods=pdo_get('jy_weishop_goods',array('id'=>$g['goodsid']));
					$g['unit']=$goods['unit'];
					$g['weight']=pdo_getcolumn('jy_weishop_goods_dispatch',array('goodsid'=>$g['goodsid']),'weight');
					if($g['optionid']){
						$specs = pdo_getcolumn('jy_weishop_goods_option',array('goodsid'=>$g['goodsid'],'id'=>$g['optionid']),'specs');
						$g['price']=pdo_getcolumn('jy_weishop_goods_option',array('goodsid'=>$g['goodsid'],'id'=>$g['optionid']),'price');
						$specs = explode("_", $specs);
						$specsall = pdo_getall('jy_weishop_goods_spec_item',array('id'=>$specs,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
						if($specsall){
							$optiontitle='';
							foreach($specsall as $k=>&$sp){
								if($k==0){
									$optiontitle .=$sp['title'];
								}else{
									$optiontitle .='，'.$sp['title'];
								}
							}
						}
						$g['optiontitle']= $optiontitle;
					}
					$g['totalprice']=$g['price'];
					$g['goods_title']=$goods['title'];
					$item['goods_num'] +=$g['goods_num'];
					$g['image']=tomedia($goods['image']);
				}
			}
			$selfinfo=pdo_get('jy_weishop_order_self',array('orderid'=>$item['id']));
			$selfinfo['selftime']=$selfinfo['selftime']?date('Y-m-d H:i:s',$selfinfo['selftime']):'尽快配送';
			$selfinfo['selftime']=$selfinfo['out_time']?:$selfinfo['selftime'];
			$item['selfinfo']=$selfinfo;
			$item['total']=pdo_getsum('jy_weishop_order_goods',array('orderid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'total');
		}
	}
	$return['order_list']=$return['list']=$order_list;
	$return['isopenvoice']=$this->session('isopenvoice')?1:0;
	exi($return);
}

//订单详情
if($_RQ['op']=='detail'){
	if(!$_RQ['id']){
	exi('订单不存在，请重新选择','error');
	}
	$order=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	//用户信息
	$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['mid']));
	$user=pdo_get('jy_weishop_member',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['mid']));
	//项目信息
	$store=pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['store_id']));	
	$store_times=json_decode($store['serve_time'],true);
	$order['serve_time']=$store_times[$order['timeid']];
	$order['address']=json_decode($order['address'],true);
	if($order['address'] && ($order['address']['name'] || $order['address']['realname'])){
		$address=$order['address'];
	}else{
		$address=pdo_get('core_address',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['addressid']));
	}
	if(!is_array($address['pcd'])){
		$address['pcd']=json_decode($address['pcd'],true);
	}
	$address['name']=$address['name']?:$address['realname'];
	$order['paytime']=$order['paytime']?date('Y-m-d H:i:s', $order['paytime']):'';
	$order['addresstext']=model('core/member')->get_address_str($address);
	$order['order_out']=$address;
	//自定义字段数据
	$return['order']=$order;
	$return['user']=$user;
	$return['member']=$member;
	$return['serve']=$store;
	exi($return);
}
//员工确认订单
if($_RQ['op']=='getorder'){
	$order_id=intval($_RQ['id']);
	if(!$order_id){
		exi('订单不存在，请重新选择订单','error');
	}
	$params=array('order_id'=>$order_id,'peisong_id'=>$_SESSION['uid']);
	$this->model('order')->order_get($params);
	exi('接收配送任务成功!','','/worker_order/');
}
//员工取消订单
if($_RQ['op']=='unreceive'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	pdo_update('jy_weishop_order',array('status'=>'1'),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	exi('取消订单成功');
}